<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\Config;
use App\Models\System\Email;
use App\Models\System\EmailHair;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;

class ConfigService
{
    use Service;

    /**
     * 系统配置
     *
     * @return mixed
     */
    public function ConfigIndexService()
    {
        $emailHair = EmailHair::FindModel([ 'imap', 'server', 'port', 'sender', 'name', 'username', 'password' ]);
        $page      = file_get_contents(config_path('page.php'));
        $name      = [];
        return view('system.config.index', compact('emailHair', 'name', 'page'));
    }

    /**
     * 系统配置修改
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ConfigStoreService( $request )
    {
        $error          = $this->getErrorCode();
        $adminValidator = $this->getAdminValidator();
        $status         = $request->input('static', 1);
        switch ( $request->input('status') ) {
            case 1:
                $validator = $adminValidator->ConfigStoreInfoValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $box    = $request->input('box', '');
                $config = Config::FindModel([ 'status' => $status ], [], [ 'box' ]);
                if ( $config->box != $box && $box != '' ) {
                    $json = Cookie::get('username_' . $request->input('submit'));
                    if ( ! $json ) return back()->with('error', $error::message(1030));
                    $code = explode(',', $json);
                    if ( $request->input('code') != $code[ 0 ] ) return back()->with('error', $error::message(1021));
                    if ( $box != $code[ 1 ] || Auth::id() != $code[ 2 ] ) return back()->with('error', $error::message(1022));
                }//选择性验证
                $data[ 'box' ]        = $request->input('box');
                $data[ 'url' ]        = $request->input('url');
                $data[ 'name' ]       = $request->input('name');
                $data[ 'record' ]     = $request->input('record');
                $data[ 'info' ]       = $request->input('info');
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                Config::UpdateModel([ 'status' => $status ], $data);
                break;
            case 2:
                $validator = $adminValidator->ConfigStoreTitleValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $data[ 'title' ]      = $request->input('title');
                $data[ 'word' ]       = $request->input('word');
                $data[ 'riptive' ]    = $request->input('riptive');
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                Config::UpdateModel([ 'status' => $status ], $data);
                break;
            case 3:
                $validator = $adminValidator->ConfigStoreServerValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $email  = Email::FindModel([ 'sender' ]);
                $sender = $request->input('sender');
                if ( $email->sender != $sender && $sender != '' ) {
                    $json = Cookie::get('username_' . $request->input('submit'));//获取Cookie信息
                    if ( ! $json ) return back()->with('error', $error::message(1030));
                    $code = explode(',', $json);
                    if ( $request->input('code') != $code[ 0 ] ) return back()->with('error', $error::message(1021));
                    if ( $sender != $code[ 1 ] || Auth::id() != $code[ 2 ] ) return back()->with('error', $error::message(1022));
                }
                $data[ 'sender' ]     = $request->input('sender');
                $data[ 'server' ]     = $request->input('server');
                $data[ 'port' ]       = $request->input('port');
                $data[ 'name' ]       = $request->input('name');
                $data[ 'username' ]   = $request->input('username');
                $data[ 'password' ]   = $request->input('password');
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                Email::UpdateModel($data);
                $base = base_path();
                exec("php $base/artisan config:cache");
                break;
            case 4:
                $validator = $adminValidator->ConfigStoreCompanyValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $data[ 'company' ]    = $request->input('company');
                $data[ 'qq' ]         = $request->input('qq');
                $data[ 'phone' ]      = $request->input('phone');
                $data[ 'fixed' ]      = $request->input('fixed');
                $data[ 'fax' ]        = $request->input('fax');
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                Config::UpdateModel([ 'status' => $status ], $data);
                break;
            case 5:
                $validator = $adminValidator->ConfigStoreSerValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $emailHair = EmailHair::FindModel([ 'sender' ]);
                $sender    = $request->input('sender');
                if ( $emailHair->sender != $sender && $sender != '' ) {
                    $json = Cookie::get('username_' . $request->input('submit'));//获取Cookie信息
                    if ( ! $json ) return back()->with('error', $error::message(1030));
                    $code = explode(',', $json);
                    if ( $request->input('code') != $code[ 0 ] ) return back()->with('error', $error::message(1021));
                    if ( $sender != $code[ 1 ] || Auth::id() != $code[ 2 ] ) return back()->with('error', $error::message(1022));
                }
                $data[ 'sender' ]     = $request->input('sender');
                $data[ 'server' ]     = $request->input('server');
                $data[ 'port' ]       = $request->input('port');
                $data[ 'name' ]       = $request->input('name');
                $data[ 'username' ]   = $request->input('username');
                $data[ 'password' ]   = $request->input('password');
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                EmailHair::UpdateModel($data);
                break;
            case 6:
                if ( ! $request->filled('page') ) return back()->with('error', $error::message(1001));
                $page = $request->input('page');
                file_put_contents(config_path('page.php'), $page);
                $path = base_path('bootstrap/cache/config.php');
                @unlink($path);
                $base = base_path();
                exec("php $base/artisan config:cache");
                break;
            default:
                return back()->with('error', $error::message(1001));
                break;
        }
        return back()->with('success', $error::message(203));
    }

}
